package com.itheima.tanhua.apiImpl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.tanhua.pojo.mongo.Movement;
import com.itheima.tanhua.api.mongodb.MovementApi;
import com.itheima.tanhua.util.IdWorker;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class MovementApiImpl implements MovementApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;

    @Override
    public Movement saveMovements(Movement movement) {
        //处理数据 给movement 添加pid
        movement.setPid(idWorker.getNextId("movement"));
        //数据处理完成  实行添加操作
        return  mongoTemplate.save(movement);
    }

    /**
     * 查询所有的动态,并进行分页操作
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    public List<Movement> findMovementAll(Long userId, Integer page, Integer pagesize) {
        /**
         * 思路分析
         * 从动态集合中查询所有的动态
         */
        //查询所有动态  分页排序
        Pageable pageAble=PageRequest.of(page-1,pagesize, Sort.by(Sort.Order.desc("created")));
        Query query=Query.query(Criteria.where("userId").is(userId)).with(pageAble);
        return mongoTemplate.find(query, Movement.class);

    }

    /**
     * 查询ids携带的所有id的动态  并分页
     * @param ids
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<Movement> findMovementById(List<Object> ids, Integer page, Integer pagesize) {

        Pageable pageAble=PageRequest.of(page-1,pagesize, Sort.by(Sort.Order.desc("created")));
        Query query=Query.query(Criteria.where("id").in(ids)).with(pageAble);
        return mongoTemplate.find(query, Movement.class);

    }

    /**
     * 通过pids查询动态的集合对象
     * @param pids
     * @return
     */
    @Override
     public List<Movement> findMovementByPid(List<Long> pids){
        Query query=Query.query(Criteria.where("pid").in(pids));
        return mongoTemplate.find(query, Movement.class);

    }

    @Override
    public List<Movement> randomMovements(Integer counts) {
        TypedAggregation aggregation= Aggregation.newAggregation(Movement.class,Aggregation.sample(counts));
        AggregationResults<Movement> aggregate = mongoTemplate.aggregate(aggregation, Movement.class);
        return aggregate.getMappedResults();
    }

    /**
     * 通过movementId查询动态的对象
     * @param
     * @return
     */
    @Override
    public Movement findOneByMoventId(String moventId) {
        //将string的id转化为
//        ObjectId moventId = new ObjectId(id);
//        Query query=Query.query(Criteria.where("moventId").is(moventId));
        return mongoTemplate.findById(moventId,Movement.class);
    }
    /**
     * 动态点赞+1
     *
     * @param movementId 动态id
     * @return
     */
    @Override
    public Integer updateLikeCount(String movementId) {
        ObjectId objectId = new ObjectId(movementId);
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        Integer likeCount = movement.getLikeCount();
        if (ObjectUtil.isEmpty(likeCount)){
            likeCount=0;
        }
        likeCount=likeCount+1;
        Update update=new Update();
        update.set("likeCount",likeCount);
        Query query = Query.query(Criteria.where("_id").is(objectId));
        mongoTemplate.updateFirst(query,update,"movement");

        return likeCount;
    }

    @Override
    public Integer updateDisLike(String movementId) {
        ObjectId objectId = new ObjectId(movementId);
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        Integer likeCount = movement.getLikeCount();
        if (ObjectUtil.isEmpty(likeCount)){
            likeCount=1;
        }
        likeCount=likeCount-1;
        Update update=new Update();
        update.set("likeCount",likeCount);
        Query query = Query.query(Criteria.where("_id").is(objectId));
        mongoTemplate.updateFirst(query,update,"movement");
        return likeCount;
    }

    @Override
    public Integer updateLoveCount(String movementId) {
        ObjectId objectId = new ObjectId(movementId);
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        Integer loveCount = movement.getLoveCount();
        if (ObjectUtil.isEmpty(loveCount)){
            loveCount=0;
        }
        loveCount=loveCount+1;
        Update update=new Update();
        update.set("loveCount",loveCount);
        Query query = Query.query(Criteria.where("_id").is(objectId));
        mongoTemplate.updateFirst(query,update,"movement");

        return loveCount;
    }

    @Override
    public Integer updateDis(String movementId) {
        ObjectId objectId = new ObjectId(movementId);
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        Integer loveCount = movement.getLoveCount();
        if (ObjectUtil.isEmpty(loveCount)){
            loveCount=1;
        }
        loveCount=loveCount-1;
        Update update=new Update();
        update.set("loveCount",loveCount);
        Query query = Query.query(Criteria.where("_id").is(objectId));
        mongoTemplate.updateFirst(query,update,"movement");
        return loveCount;
    }

   /* //所有当前用户的通过审核的动态
    @Override
    public List<Movement> findMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        Pageable pageAble=PageRequest.of(page-1,pagesize, Sort.by(Sort.Order.desc("created")));
        Query query=Query.query(Criteria.where("userId").is(uid).and("state").in(2,5)).with(pageAble);
        return mongoTemplate.find(query, Movement.class);
    }*/
}
